package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template38 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("32KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("32KEY-3")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",38,e);
                }
            }
        }
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey;

        if (plot.getBarChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV().equals("338")){
            jsonKey = "userAge";
        }else {
            jsonKey = "userArea";
        }
        List<SeriesData> barData = getData(data, jsonKey);
        templateUtil.barChart(plot,barData,workbook,sheet,chart);
    }

    private List<SeriesData> getData(JsonNode data, String jsonKey) {
        JsonNode jsonNode = data.get(jsonKey);
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> nameDoubles = new ArrayList<>();
        for (JsonNode node : jsonNode) {
            NameDouble nameDouble = new NameDouble(node.get("tempName").asText(),node.get("tempCount").asDouble());
            nameDoubles.add(nameDouble);
        }
        nameDoubles.sort(Comparator.comparing(NameDouble::getValue).reversed());
        SeriesData seriesData = new SeriesData("",nameDoubles,"");
        seriesDatas.add(seriesData);
        return seriesDatas;
    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
        //遍历获取每一个单元格
        for (int col =0 ;col < numberOfColumns; col++){
            for (int row = 0; row<numberOfRows; row++ ){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){

                        if (col ==0){
                            if (row ==0){
                                String s = data.get("clockInCount").get(0).get("date").asText();
                                cellParagraphs.getTextRuns().get(0).setText(s);
                                continue;
                            }
                            if (row ==4){
                                String s = data.get("clockInCount").get(1).get("date").asText();
                                cellParagraphs.getTextRuns().get(0).setText(s);
                                continue;
                            }
                            continue;
                        }
                        if (row == 0){
                            continue;
                        }
                        if (row == 4){
                            continue;
                        }
                        cellParagraphs.getTextRuns().get(0).setText(getCellData(data,row,col));
                    }
                }
            }
        }
    }

    private String getCellData(JsonNode data, int row, int col) {
        JsonNode jsonNode;
        if (row < 4){
            jsonNode = data.get("clockInCount").get(0);
        }else {
            jsonNode = data.get("clockInCount").get(1);
        }
        for (JsonNode node : jsonNode.get("beens")) {
            if (node.get("name").asText().equals(getColName(col))){
                return node.get(getRowName(row)).asText();
            }
        }
        return "";
    }

    private String getColName(int col){
        String colName;
        switch (col){
            case 1:
                colName = "其他";
                break;
            case 2:
                colName = "跑步";
                break;
            case 3:
                colName = "篮球";
                break;
            case 4:
                colName = "健身";
                break;
            case 5:
                colName = "足球";
                break;
            case 6:
                colName = "羽毛球";
                break;
            default:
                colName = "";
                break;
        }
        return colName;
    }

    private String getRowName(int row){
        String rowName;
        switch (row){
            case 1:
                rowName = "count";
                break;
            case 2:
                rowName = "users";
                break;
            case 3:
                rowName = "avg";
                break;
            case 5:
                rowName = "count";
                break;
            case 6:
                rowName = "users";
                break;
            case 7:
                rowName = "avg";
                break;
            default:
                rowName = "";
                break;
        }
        return rowName;
    }

    public void pieChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey = "userGender";
        List<SeriesData> pieData = getData(data,jsonKey);
        templateUtil.pieChart(plot,pieData,workbook,sheet,chart);
    }

    public void simpleShape(int red, XSLFShape shape, JsonNode data) {
        if (red ==0 || red %10 !=0){
            return;
        }
        String jsonKey1 = "";
        String name = "";
        switch (red){
            case 10:
                jsonKey1 = "count";
                name = "其他";
                break;
            case 20:
                jsonKey1 = "users";
                name = "其他";
                break;
            case 30:
                jsonKey1 = "count";
                name = "跑步";
                break;
            case 40:
                jsonKey1 = "users";
                name = "跑步";
                break;
            case 50:
                jsonKey1 = "count";
                name = "篮球";
                break;
            case 60:
                jsonKey1 = "users";
                name = "篮球";
                break;
            case 70:
                jsonKey1 = "count";
                name = "健身";
                break;
            case 80:
                jsonKey1 = "users";
                name = "健身";
                break;
            case 90:
                jsonKey1 = "count";
                name = "足球";
                break;
            case 100:
                jsonKey1 = "users";
                name = "足球";
                break;
            case 110:
                jsonKey1 = "count";
                name = "羽毛球";
                break;
            case 120:
                jsonKey1 = "users";
                name = "羽毛球";
                break;
            default:
                break;
        }
        int trend = getSimpleData(data,jsonKey1,name);
        templateUtil.replaceSimpleShape(trend,shape);
    }

    private int getSimpleData(JsonNode data, String jsonKey1, String name) {
        JsonNode oldNode = data.get("clockInCount").get(0);
        JsonNode newNode = data.get("clockInCount").get(1);

        double oldNum = 0;
        double newNum = 0;
        for (JsonNode node : oldNode.get("beens")) {
            if (node.get("name").asText().equals(name)){
                oldNum = node.get(jsonKey1).asDouble();
            }
        }
        for (JsonNode node : newNode.get("beens")) {
            if (node.get("name").asText().equals(name)){
                newNum = node.get(jsonKey1).asDouble();
            }
        }

        if (newNum > oldNum){
            return 2;
        }
        if (newNum < oldNum){
            return 3;
        }
        return 1;
    }
}
